#include <stdio.h>
#include <stdlib.h>

typedef struct queue
{
    int *q;
    int queueSize;
    int head;
    int tail;
}queue;

void init_queue(queue *q, int size)
{
	q->queueSize=size;
    q->q = (int*)malloc(sizeof(int) * q->queueSize);
    q->head=0;
    q->tail=0;
}

void enqueue(queue* q,int x)
{
    if(((q->tail+1) % q->queueSize) == q->head)
    {
        printf("queue is full.\n");
    }
    else
    {
        q->q[q->tail] = x;
        q->tail = (q->tail+1) % (q->queueSize);
    }
}

int dequeue(queue* q)
{
	int value;
    if(q->tail == q->head)
	{
		printf("queue is empty.\n");
	}
    else
    {
        value = q->q[q->head];
		q->head=q->head + 1;
        q->head = (q->head % q->queueSize);
    }
	return value;
}


int main()
{
    queue q;
	init_queue(&q, 3);
    enqueue(&q,10);
    enqueue(&q,20);
    printf("head=%d\t tail=%d\n",q.head,q.tail);
	printf("%d\n",dequeue(&q));
	printf("%d\n",dequeue(&q));

	return 0;
}